﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;

namespace ProjectCore.Infrastructure.DomainService
{
    public class AdminDomainService : IAdminDomainService
    {
        private readonly IBaseRepository<Admin> baseRepository;
        private readonly MyContext myContext;
        public AdminDomainService(IBaseRepository<Admin> _baseRepository, MyContext _myContext)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
        }


        /// <summary>
        /// 获取管理员数据
        /// </summary>
        /// <param name="na"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<HeaderResult<List<Admin>>> GetAdmins(string na, int pageIndex, int pageSize)
        {
            try
            {
                Expression<Func<Admin, bool>> where = e => true;
                if (!string.IsNullOrEmpty(na))
                {
                    where = where.And(e => e.AdminName.Contains(na));
                }

                var adminList = await baseRepository.LoadEntityListAsync(where, e => e.Sort, "asc", pageIndex, pageSize);
                int count = await baseRepository.GetEntitiesCountAsync(where);
                HeaderResult<List<Admin>> result = new HeaderResult<List<Admin>>()
                {
                    IsSucceed = true,
                    Result = adminList,
                    Total = count,
                };
                return result;
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public async Task<int> AddAdmin(Admin admin)
        {
            try
            {
                return await baseRepository.AddEntityAsync(admin);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
